#Sean Breid: POS Scorer. The problem that this code faces is scoring a POS
#tagger. It will input a file that has all of the correct information and a file
#that has predicted the pos tags and it will compare the two and output the
#overall percentage correct to a file. Secondly, it will output a confusion
#matrix which includes will include the following information: how many times
#the tagger confused a tag with another tag. The input of this file will be 
#"perl scorer.pl <file-with-correct-tags> <file-with-predicted> > <output-file>"
#the results including the overall percentage and the confusion matrix will be
#written to the output-file. The confusioin matrix will be in this format. For
#each type of mistake: "predicted: <predicted-tag> actual: <actual-tag>
#occurances: <no.-of-times-mistake-seen>". At the beginning of this file it
#formats the data and reads it in. It then reads every tag into an array. Then 
#formats and reads in the data from the second file and checks each tag against
#the corresponding tag in the array.  For each tag in the second file that
#the tag in the array it will add one to correct. For each tag whether right or
#wrong it will add one to the total. For each tag that doesn't match the tag in
#the array we will put the predicted tag as a key to a hash which hashes to a 
#hash whose key is the tag from the array which hashes to the number of
#occurances we have seen that incorrect combination. At the end we will print
#out the correct divided by the total which is the overall percentage and then
#it prints out the hashes of hashes which now will contain the confusion matrix

initialize();


while(<>){

    my @current_line = format_line($_);#formats the line

    parse_tags(\@current_line);#parses the training data file

    last if eof;#breaks when reached the end of the traning data file


}

 #while (($key, $value) = each(%pos_hash)){
    #print $key."~> ".$value."\n";
#   while(($key2, $value2) = each %$value){
#	print $key."~> ".$key2."~> ".$value2."\n";

#   }
#}


while(<>){
    
    my @current_line = format_line($_);

    $correct_tags += check_tags(\@current_line);

    #print $correct_tags."\n";

    #print "@current_line";

}

print "The overall percentage is ".($correct_tags/$tag_count).".\n";

while (($key, $value) = each(%confusion_matrix)){
    #print $key."~> ".$value."\n";
   while(($key2, $value2) = each %$value){
	print $key."~> ".$key2."~> ".$value2."\n";

   }
}


################################################################################
#initializes all of the data structures for the program

sub initialize{

    @tags_array = ();

    %confusion_matrix;

    $tag_count = 0;

    $correct_tags = 0;

}

################################################################################
#formats the line for into an array of tokens
#param 1: the string of the line to be formatted

sub format_line{

    chomp;#removes newline characters

    s/(\[|\])/ $1 /g;#puts spaces between punctutation

    my @line = split;#splits up all of the tokens

    return @line;

}


################################################################################
#parses the tags from a file
#param 1: the line to be parsed

sub parse_tags{

 my @my_line = @{ $_[0] };#convert the reference to the array into an array

    foreach $word(@my_line){#for every token in this line

	if($word =~ /(.*)\/(.*)/){#split the word from the tag

	    #print "word: ".$1." tag: ".$2."\n";

	    push(@tags_array, $2);

	}

    }

}


################################################################################
#checks the tags from the key file with the tags in @tags_array
#param 1: the line to be checked
#returns how many were correct
#also adds to the total number of tags

sub check_tags{

 my @my_line = @{ $_[0] };#convert the reference to the array into an array

 my $correct = 0;

 foreach $word(@my_line){#for every token in this line

     if($word =~ /(.*)\/(.*)/){#split the word from the tag

	 my $tag = $2;

	 my $ww = $1;

	 #if($tag_count < 20){

	 #print "word1: ".$tags_array[$tag_count]." word2: ".$2."\n";

	 #}

	 if($tags_array[$tag_count] =~ /(.*)\|(.*)/){#if the tag has a '|' in it
	    
	      if($1 eq $tag){
	     
		 $correct += 1;#if we are correct add one to the correct total

	      }
	      elsif($2 eq $tag){

		  $correct += 1;#if we are correct add one to the correct total

	      }
	      else{
		  #if we get it wrong add an occurance to the confusion matrix hash
		  if($confusion_matrix{$tag}{$tags_array[$tag_count]}){
		      #if hash entry already exists add one occurance
		      $confusion_matrix{$tag}{$tags_array[$tag_count]} += 1;

		  }else{#hash entry does not exist already

		     $confusion_matrix{$tag}{$tags_array[$tag_count]} = 1;

		  }
	      }

	 }
	 else{

	     if($tag eq $tags_array[$tag_count]){

		 $correct += 1;#if we are correct add one to the correct total


	     }
	     else{
		 #if we get it wrong add an occurance to the confusion matrix hash

		 if($tag eq "NN" && $tags_array[$tag_count] eq "NNS"){

		     print $ww."\n";

		 }

		 if($confusion_matrix{$tag}{$tags_array[$tag_count]}){
		     #if hash entry already exists add one occurance
		     $confusion_matrix{$tag}{$tags_array[$tag_count]} += 1;

		 }else{#hash entry does not exist already

		     $confusion_matrix{$tag}{$tags_array[$tag_count]} = 1;

		 }
	     }

	 }

	 $tag_count += 1;

     }

 }

 return $correct;

}
